package com.hunlian.makelove.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hunlian.makelove.AppApplication;
import com.hunlian.makelove.R;
import com.hunlian.makelove.bean.CheckPhoneRegisteredBean;
import com.hunlian.makelove.common.AppConfig;
import com.hunlian.makelove.common.AppUtils;
import com.hunlian.makelove.net.NetVisitCallback;
import com.hunlian.makelove.net.api.CheckPhoneRegisteredServiceVisit;
import com.hunlian.makelove.tools.MyToast;

public class CheckIsRegisterActivity extends BaseActivity{

	private CheckIsRegisterActivity mSelf;

	private String mStrPhone;
	private EditText edtUserTel;
	private Button btnNext;
	private RelativeLayout layDeleteTel;
	private String mFrom;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mSelf = this;
		setContentView(R.layout.check_is_register_activity);
		
		mFrom = getIntent().getStringExtra("from");
		
		ImageButton mIbBack = (ImageButton) findViewById(R.id.back);
		mIbBack.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				mSelf.finish();
			}
			
		});
		TextView mTvTitle = (TextView) findViewById(R.id.title);
		if(mFrom!=null&&"register".equals(mFrom)){
			mTvTitle.setText("注册");
		}else if(mFrom!=null&&"forgetPassword".equals(mFrom)){
			mTvTitle.setText("找回密码");
		}
		
		
		// 标题下方控件声明
		edtUserTel = (EditText) this.findViewById(R.id.edtUserTel);
		edtUserTel.setHintTextColor(getResources().getColor(R.color.light_txt_color));
		layDeleteTel = (RelativeLayout) this.findViewById(R.id.layDeleteTel);
		btnNext = (Button) this.findViewById(R.id.btnNext);


		
		setListener();
	}

	private void setListener() {

		// 清空电话号码
		layDeleteTel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				edtUserTel.setText("");
			}
		});
		
		// 判断电话是否符合标准并跳转下一界面并将正确的电话号码传递到下一个界面,并调用获取验证码接口
		// 并判断此手机号是不是已经注册过的
		btnNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (TextUtils.isEmpty(edtUserTel.getText().toString())) {
					Toast.makeText(mSelf, "请输入手机号",
							Toast.LENGTH_SHORT).show();
					return;
				}
				if (!AppUtils.isMobileNO(edtUserTel.getText().toString())) {
					Toast.makeText(mSelf,
							"请输入正确格式的手机号码", Toast.LENGTH_SHORT).show();
					return;
				}
				// 密码找回不需要判断手机号是否已经注册直接获取验证码
				checkTel() ;
			}
		});

	}
	
	/**
	 * 验证手机号是否已经注册过了
	 */

	private void checkTel() {
		if (!AppApplication.networkAvailable) {
			MyToast.showShort("没有网络");
			return;
		}

		CheckPhoneRegisteredServiceVisit visit = new CheckPhoneRegisteredServiceVisit(mSelf);
		visit.setParamters(edtUserTel.getText().toString());
		System.out.println("手机号是否注册参数：" + visit.getParamter());
		visit.getResult(new NetVisitCallback<CheckPhoneRegisteredBean,Object>(mSelf){

			@Override
			public void onSuccess(CheckPhoneRegisteredBean data) {

				if(data==null)return;
				String status = data.getStatus();
				//status = "1";
				if(status.equals("1")){
					//Toast.makeText(mSelf, "帐号未注册", Toast.LENGTH_SHORT).show();
					//清空自动登录的帐号和密码
					AppConfig.getInstance().clearLoginedUserId();

					Intent intent = new Intent(mSelf,SmsVerifyActivity.class);
					intent.putExtra("phone",edtUserTel.getText().toString());
					intent.putExtra("from", "register");
					startActivity(intent);
					overridePendingTransition(R.anim.right_in, R.anim.left_out);
				}else if(status.equals("2")){
					Toast.makeText(mSelf, "帐号已禁用", Toast.LENGTH_SHORT).show();
				}else if(status.equals("3")){//已经注册
					if(mFrom.endsWith("forgetPassword")){
						//Toast.makeText(mSelf, "来自忘记密码", Toast.LENGTH_SHORT).show();
						Intent intent = new Intent(mSelf,SmsVerifyActivity.class);
						intent.putExtra("phone", edtUserTel.getText().toString());
						intent.putExtra("from", "forget");
						startActivity(intent);
						overridePendingTransition(R.anim.right_in, R.anim.left_out);
					}else if(mFrom.endsWith("register")){
						Toast.makeText(mSelf, "帐号已经注册", Toast.LENGTH_SHORT).show();
					}
				}
				
			}

			@Override
			public void onFailure(String errorEvent, String message,Object obj) {
				super.onFailure(errorEvent, message,obj);

			}
			
		});
	}

}
